/**
 * 
 */
package com.ece.core.teamcollabexplorer;

import java.util.UUID;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This class takes care of all the logic associated with 
 * Scheduling a new code review.
 * 
 * @author Keyur Hindocha
 */
public class ScheduleReviewAction implements IObjectActionDelegate {
	String value;

	@Override
	public void run(IAction action) {
		// FIXME : Add the received input to the file and send it across.
		System.out.println(open());
	}
	
	/**
	   * Makes the dialog visible and display it in the IDE.
	   * 
	   * @return
	   */
	  public String open() {
	    final Shell shell = new Shell( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
	    shell.setText("Schedule Review Dialog");

	    shell.setLayout(new GridLayout(2, true));

	    Label label = new Label(shell, SWT.NULL);
	    label.setText("Please enter the name of the reviewer:");

	    final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);

	    final Button buttonOK = new Button(shell, SWT.PUSH);
	    buttonOK.setText("OK");
	    buttonOK.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    Button buttonCancel = new Button(shell, SWT.PUSH);
	    buttonCancel.setText("Cancel");

	    text.addListener(SWT.Modify, new Listener() {
	      public void handleEvent(Event event) {
	        try {
	          value = text.getText();
	          buttonOK.setEnabled(true);
	        } catch (Exception e) {
	          buttonOK.setEnabled(false);
	        }
	      }
	    });

	    buttonOK.addListener(SWT.Selection, new Listener() {
	      public void handleEvent(Event event) {
	        shell.dispose();
	      }
	    });

	    buttonCancel.addListener(SWT.Selection, new Listener() {
	      public void handleEvent(Event event) {
	        value = null;
	        shell.dispose();
	      }
	    });
	    
	    shell.addListener(SWT.Traverse, new Listener() {
	      public void handleEvent(Event event) {
	        if(event.detail == SWT.TRAVERSE_ESCAPE)
	          event.doit = false;
	      }
	    });

	    text.setText("");
	    shell.pack();
	    shell.open();

	    Display display = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay();
	    while (!shell.isDisposed()) {
	      if (!display.readAndDispatch())
	        display.sleep();
	    }

	    return value;
	  }

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		
	}
	
	private String generateUniqueGUID() {
		return UUID.randomUUID().toString();
	}

}
